package com.stylefeng.guns.modular.rate.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.annotion.BussinessLog;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.modular.rate.service.IRateService;
import com.stylefeng.guns.modular.system.model.Rate;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * 费率管理控制器
 *
 * @author fengshuonan
 * @Date 2018-07-07 13:45:01
 *
 */
@Controller
@RequestMapping("/rate")
public class RateController extends BaseController {

    private String PREFIX = "/rate/rate/";

    @Autowired
    private IRateService rateService;

    /**
     * 跳转到费率管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "rate.html";
    }

    /**
     * 跳转到添加费率管理
     */
    @RequestMapping("/rate_add")
    public String rateAdd() {
        return PREFIX + "rate_add.html";
    }

    /**
     * 跳转到修改费率管理
     */
    @RequestMapping("/rate_update/{rateId}")
    public String rateUpdate(@PathVariable Integer rateId, Model model) {
        Rate rate = rateService.selectById(rateId);
        model.addAttribute("item",rate);
        LogObjectHolder.me().set(rate);
        return PREFIX + "rate_edit.html";
    }

    /**
     * 获取费率管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String time) {

        EntityWrapper<Rate> wrapper = new EntityWrapper<Rate>();
        if (StringUtils.isNotBlank(time)) {
            wrapper.eq("time", time);
        }
        wrapper.orderBy("time",false);

        return rateService.selectList(wrapper);
    }

    /**
     * 新增费率管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @BussinessLog(value = "新增费率管理")
    public Object add(Rate rate) {
        Date date = new Date();
        rate.setUpdatetime(date);
        rate.setCreattime(date);
        rateService.insert(rate);
        return SUCCESS_TIP;
    }

    /**
     * 删除费率管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @BussinessLog(value = "删除费率管理")
    public Object delete(@RequestParam Integer rateId) {
        rateService.deleteById(rateId);
        return SUCCESS_TIP;
    }

    /**
     * 修改费率管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @BussinessLog(value = "修改费率管理")
    public Object update(Rate rate) {
        rateService.updateById(rate);
        return SUCCESS_TIP;
    }

    /**
     * 费率管理详情
     */
    @RequestMapping(value = "/detail/{rateId}")
    @ResponseBody
    public Object detail(@PathVariable("rateId") Integer rateId) {
        return rateService.selectById(rateId);
    }


}
